package com.star.exam;

public class Solution1 {

    // 删除所有含有重复数字的节点
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        boolean flag = false;
        while (head != null && head.next != null && head.val == head.next.val) {
            head = head.next;
            flag = true;
        }
        if (flag) head = head.next;
        if (head != null && flag)
            head = deleteDuplicates(head);
        else if (head != null)
            head.next = deleteDuplicates(head.next);
        return head;
    }


    public static void main(String[] args) {
        ListNode list = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(4);
        ListNode node6 = new ListNode(5);
        list.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        ListNode node = deleteDuplicates(list);

        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
